//
//  HMPictureCView.swift
//  sina
//
//  Created by Kent on 16/9/27.
//  Copyright © 2016年 Ming. All rights reserved.
//

import UIKit

private let HMPictureCellID = "HMPictureCellID"

class HMPictureCView: UICollectionView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var pictureInfos: [HMStatusPicInfo]? {
        didSet {
            
            reloadData()
        }
    }
    
    var flowLayout: UICollectionViewFlowLayout?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        self.isUserInteractionEnabled = false
        self.register(HMPictureCell.self, forCellWithReuseIdentifier: HMPictureCellID)
        
        flowLayout = layout as? UICollectionViewFlowLayout
    
        self.dataSource = self

    }

}

extension HMPictureCView: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfos?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HMPictureCellID, for: indexPath) as! HMPictureCell
        
        cell.pictureInfo = pictureInfos![indexPath.item]
        
        return cell
    }

}


class HMPictureCell: UICollectionViewCell {
    
    var pictureInfo: HMStatusPicInfo? {
        didSet {
            let url = URL(string: pictureInfo?.thumbnail_pic ?? "a")
            iv.sd_setImage(with: url)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(iv)
        
        iv.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var iv: UIImageView = {
       
        let i = UIImageView()
        i.contentMode = .scaleAspectFill
        i.clipsToBounds = true
        return i
        
    }()
   
}





























